/**
 * @Author : JiGuangJie
 * @Date : 2023/4/13
 * @Time : 下午1:44
 * @Version : 1.0.0
 * @description :
 */
import {createSource} from './factory';

interface IUrl {
    img: string;
    cia: string;
}

interface IDic {
    [key: string]: IUrl;
}

const xyz: IUrl = {
    img: `http://t{0-6}.tianditu.com/DataServer?T=img_w&tk=a6bf5a2496115f58481b947040c83ae3&x={x}&y={y}&l={z}`,
    cia: `http://t{0-6}.tianditu.com/DataServer?T=cia_w&tk=a6bf5a2496115f58481b947040c83ae3&x={x}&y={y}&l={z}`
};

const wmts: IUrl = {
    img: `http://t0.tianditu.gov.cn/vec_w/wmts?tk=695f6f53c1c0087ed8ab5b52f7c446bb`,
    cia: `http://t0.tianditu.gov.cn/cva_w/wmts?tk=695f6f53c1c0087ed8ab5b52f7c446bb`
};

const urlDic: IDic = {
    xyz,
    wmts
};

// @ts-ignore
const projection: any = ol.proj.get('EPSG:3857');

const createImageLayer = (type: string) => {
    // @ts-ignore
    return new ol.layer.Tile({
        source: createSource(type, urlDic[type].img, EPSG_CODE['EPSG:3857'], {
            layer: 'vec',
            matrixSet: 'w',
            format: 'tiles',
            style: 'default'
        })
    });
};

const createCiaLayer = (type: string) => {
    // @ts-ignore
    return new ol.layer.Tile({
        source: createSource(type, urlDic[type].cia, EPSG_CODE['EPSG:3857'], {
            layer: 'cva',
            matrixSet: 'w',
            format: 'tiles',
            style: 'default'
        })
    });
};

// 标绘图层
// @ts-ignore
const plotLayer: any = new ol.layer.Vector({
    // @ts-ignore
    source: new ol.source.Vector(),
    zIndex: 9999
});


const createMap = ({type, dom, center}: { type: string, dom: HTMLElement, center: number[] }): any => {
    // @ts-ignore
    return new ol.Map({
        layers: [
            createImageLayer(type),
            createCiaLayer(type),
            plotLayer
        ],
        target: dom,
        // @ts-ignore
        view: new ol.View({
            // @ts-ignore
            center: ol.proj.transform(center, 'EPSG:4326', 'EPSG:3857'),
            projection,
            zoom: 4,
            maxZoom: 19,
            minZoom: 1
        }),
        // @ts-ignore
        interactions: new ol.interaction.defaults({
            doubleClickZoom: false   //屏蔽双击放大事件
        })
    });
};

const getPlotLayer = () => plotLayer;

export {
    createMap,
    getPlotLayer
};